<?php
/**
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file license.txt or that can be found at 
 * http://code.google.com/p/billboardce/wiki/License.
 *
 * @package    Image_Module
 * @category   Model
 * @license    http://code.google.com/p/billboardce/wiki/License New BSD License
 * @version    SVN: $Id: $
 */
class Image_Module implements Plugin_Slide_Type_Interface
{
    public function getTitle()
    {
        return 'Image';
    }
    
    public function getTypeForm()
    {
        $image = new Image_Data();
        return $image->form()->__toString();
    }
    
    public function saveTypeForm($slideId, array $data)
    {
        $image = new Image_Data();
        
        $form = $image->form(array('slideId' => $slideId));
        
        if ($form->isValid($data)) {
            $existingData = $image->find($slideId);

            $data['slideId'] = $slideId;
            
            if (is_null($existingData)) {
                $image->insert($data);
            } else {
                $image->update($data, null);
            }
        }
        
        return $form->getErrorMessages();
    }
        
    public function getData($slideId)
    {
        $image = new Image_Data();
        
        $data = $image->find($slideId);
        if (is_null($data)) {
            return array();
        } else {
            return $data->toArray();
        }
    }
    
    public function getUrl($slideId)
    {
        return null;
    }
    
    public function buildSlide($view, $slideId)
    {
        $slide = $this->getData($slideId);
        
        $view->imageUrl = $slide['url'];
        $view->bgColor = $slide['bgColor'];
        $view->alignment = $slide['alignment'];
    }
}